package leetcode.code0092;

import leetcode.helper.HelpDebug;
import leetcode.helper.tree.ListNode;

public class Solution92 {
	public ListNode reverseBetween(ListNode head, int left, int right) {
		ListNode ans = new ListNode();
		ans.next = head;
		ListNode p = ans;
		int move = left;
		while (move > 1 && p != null) {
			p = p.next;
			move--;
		}
		ListNode p1 = p.next;
		ListNode pleft = p1;
		ListNode p2 = p1;
		ListNode p0 = p1;
		move = right - left;
		// [1, 2 ,3,4,5], left = 2, right = 4
		while (move > 0 && p1 != null) {
			p1 = p0.next;
			p0.next = p1.next;
			p1.next = p2;
			p2 = p1;
			move--;
		}
		p.next = p2;
		pleft.next = p0.next;
		return ans.next;
	}

	public static void main(String[] args) {
		Solution92 so = new Solution92();
		so.reverseBetween(ListNode.set(HelpDebug.str2array3("[1,2,3,4,5]")), 2, 4);

	}

}
